<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/s_l/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/s_l/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/s_l/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/s_l/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/s_l/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/s_l/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/s_l/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="java集合,">










<meta name="description" content="因为数组中存在着很多弊端，比如长度确定后不可更改，需要在内存中开辟一段连续空间的内存，所以引入集合框架">
<meta name="keywords" content="java集合">
<meta property="og:type" content="article">
<meta property="og:title" content="集合框架">
<meta property="og:url" content="http://yoursite.com/2019/05/09/集合框架/index.html">
<meta property="og:site_name" content="S.L">
<meta property="og:description" content="因为数组中存在着很多弊端，比如长度确定后不可更改，需要在内存中开辟一段连续空间的内存，所以引入集合框架">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/s_l/2019/05/09/集合框架/images/1557363996592.png">
<meta property="og:image" content="http://yoursite.com/s_l/2019/05/09/集合框架/images/1557365590916.png">
<meta property="og:updated_time" content="2019-05-09T11:15:41.201Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="集合框架">
<meta name="twitter:description" content="因为数组中存在着很多弊端，比如长度确定后不可更改，需要在内存中开辟一段连续空间的内存，所以引入集合框架">
<meta name="twitter:image" content="http://yoursite.com/s_l/2019/05/09/集合框架/images/1557363996592.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/s_l/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/05/09/集合框架/">





  <title>集合框架 | S.L</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/s_l/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">S.L</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">记录学习过程中的点滴</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/s_l/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/s_l/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/s_l/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/s_l/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/s_l/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/s_l/2019/05/09/集合框架/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏立鹏">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/s_l/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="S.L">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">集合框架</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-09T09:53:55+08:00">
                2019-05-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  931 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>因为数组中存在着很多弊端，比如长度确定后不可更改，需要在内存中开辟一段连续空间的内存，所以引入集合框架</p>
</blockquote>
<a id="more"></a>
<h2 id="集合框架概述"><a href="#集合框架概述" class="headerlink" title="集合框架概述"></a>集合框架概述</h2><h3 id="单列数据"><a href="#单列数据" class="headerlink" title="单列数据"></a>单列数据</h3><p>  在java中，集合分为两种，一种是存储单列数据，即为有序数据，有序性是指在内存中开辟的空间是一段连续的空间，这种集合框架通常由Collection接口定义。Collection下又有List和Set接口，而具体实现了List和Set的又有具体子类实现。</p>
<h4 id="Collection的家族"><a href="#Collection的家族" class="headerlink" title="Collection的家族"></a>Collection的家族</h4><p><img src="/s_l/2019/05/09/集合框架/./images/1557363996592.png" alt="Collection的子类和及其实现类"><br>    从上图中可以看出Collection是分为List和Set，和一个迭代器组成。在开始讲解其具体内容之前，需要先了解一个迭代器的概念</p>
<h4 id="迭代器"><a href="#迭代器" class="headerlink" title="迭代器"></a>迭代器</h4><p> Iterator对象称为迭代器，那么为什么要有迭代器？，主要用于遍历 Collection 集合中的元素。有了迭代器如何遍历集合中的数据呢？<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">List list=<span class="keyword">new</span> ArrayList();</span><br><span class="line">		list.add(<span class="number">1</span>);</span><br><span class="line">		list.add(<span class="number">2</span>);</span><br><span class="line">		list.add(<span class="number">33</span>);</span><br><span class="line">		list.add(<span class="number">4</span>);</span><br><span class="line">		list.add(<span class="string">"zhang"</span>);</span><br><span class="line">		list.add(<span class="string">'a'</span>);</span><br><span class="line">		<span class="comment">//使用迭代器遍历</span></span><br><span class="line">		Iterator iterator = list.iterator();</span><br><span class="line">		<span class="keyword">while</span>(iterator.hasNext()) &#123;</span><br><span class="line">			System.out.println(iterator.next());</span><br><span class="line">		&#125;</span><br></pre></td></tr></table></figure></p>
<p>所以迭代器的主要方法有三个</p>
<table>
<thead>
<tr>
<th>方法名</th>
<th>用途</th>
<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td>hasNext()</td>
<td>返回迭代器是否还有下一个元素</td>
<td>boolean</td>
</tr>
<tr>
<td>next()</td>
<td>返回迭代器中的下一个元素</td>
<td>E</td>
</tr>
</tbody>
</table>
<h4 id="Collection常用方法"><a href="#Collection常用方法" class="headerlink" title="Collection常用方法"></a>Collection常用方法</h4><p>Collection接口定义了一些集合中的常用方法，所有家族中实现了Collection接口的子类都重写了其中的实现方法，所以这些方法可以在List和Set中都可以使用</p>
<table>
<thead>
<tr>
<th>方法名</th>
<th>用途</th>
<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td>add(E e)</td>
<td>向集合中添加一个元素</td>
<td>boolean</td>
</tr>
<tr>
<td>addAll(Collection c)</td>
<td>向集合中添加一个集合</td>
<td>boolean</td>
</tr>
<tr>
<td>addAll(Collection c)</td>
<td>向集合中添加一个集合</td>
<td>boolean</td>
</tr>
<tr>
<td>clear()</td>
<td>清空集合中所有元素</td>
<td>void</td>
</tr>
<tr>
<td>contains(Object o)</td>
<td>检测集合中是否包含某一个元素</td>
<td>boolean</td>
</tr>
<tr>
<td>equals(Object o)</td>
<td>判断集合是否和o相等</td>
<td>boolean</td>
</tr>
<tr>
<td>isEmpty()</td>
<td>检测集合是否为空</td>
<td>boolean</td>
</tr>
<tr>
<td>iterator()</td>
<td>获取集合中的迭代器</td>
<td>Iterator<e></e></td>
</tr>
<tr>
<td>remove(Object o)</td>
<td>删除集合中的元素</td>
<td>boolean</td>
</tr>
<tr>
<td>removeAll(Collection&lt;?&gt; c)</td>
<td>在集合中删除一个一个集合</td>
<td>boolean</td>
</tr>
<tr>
<td>size()</td>
<td>得到集合的大小</td>
<td>int</td>
</tr>
<tr>
<td>toArray()</td>
<td>将集合转化为Object[]数组</td>
<td>Object[]</td>
</tr>
<tr>
<td>toArray(T[] a)</td>
<td>将集合转化为指定类型的数组</td>
<td><t> T[]</t></td>
</tr>
</tbody>
</table>
<h3 id="双列数据"><a href="#双列数据" class="headerlink" title="双列数据"></a>双列数据</h3><p>  双列数据，保存具有映射关系“key-value对”的集合，由Map接口定义规范，具体实现由其子类实现。</p>
<p><img src="/s_l/2019/05/09/集合框架/./images/1557365590916.png" alt="Map接口及其子类实现"></p>
<p>Map与Collection并列存在。<br>Map 中的 key 和  value 都可以是任何引用类型的数据<br>Map 中的 key 用Set来存放，不允许重复，即同一个 Map 对象所对应的类，须重写hashCode()和equals()方法<br>常用String类作为Map的“键”<br> key 和 value 之间存在单向一对一关系，即通过指定的 key 总能找到唯一的、确定的 value<br> Map接口的常用实现类：HashMap、TreeMap、LinkedHashMap和Properties。其中，HashMap是 Map 接口使用频率最高的实现类</p>
<h4 id="Map中的常用方法"><a href="#Map中的常用方法" class="headerlink" title="Map中的常用方法"></a>Map中的常用方法</h4><table>
<thead>
<tr>
<th>方法名</th>
<th>用途</th>
<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td>clear()</td>
<td>移除map中的所有元素</td>
<td>void</td>
</tr>
<tr>
<td>putAll(Map m)</td>
<td>将m中的所有key-value对存放到当前map</td>
<td>void</td>
</tr>
<tr>
<td>put(K key, V value)</td>
<td>向map中添加一个key-value</td>
<td>V</td>
</tr>
<tr>
<td>remove(Object key)</td>
<td>从map中移除指定键</td>
<td>boolean</td>
</tr>
<tr>
<td>containsKey(Object key)</td>
<td>检测集合中是否包含某个key值</td>
<td>boolean</td>
</tr>
<tr>
<td>containsValue(Object value)</td>
<td>检测集合中是否包含某个Value值</td>
<td>boolean</td>
</tr>
<tr>
<td>entrySet()</td>
<td>返回一个Map.Entry的Set集合</td>
<td>Set&lt;Map.Entry&lt;K,V&gt;&gt;</td>
</tr>
<tr>
<td>get(Object key)</td>
<td>返回key值的Value</td>
<td>V</td>
</tr>
<tr>
<td>isEmpty()</td>
<td>判断map中是否包含有key-value键值对</td>
<td>boolean</td>
</tr>
<tr>
<td>keySet()</td>
<td>返回key的Set集合</td>
<td>Set<k></k></td>
</tr>
<tr>
<td>iterator()</td>
<td>获取集合中的迭代器</td>
<td>Iterator<e></e></td>
</tr>
<tr>
<td>size()</td>
<td>得到map的大小</td>
<td>int</td>
</tr>
<tr>
<td>values()</td>
<td>得到所有Values的集合</td>
<td>Collection<v></v></td>
</tr>
</tbody>
</table>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/s_l/tags/java集合/" rel="tag"># java集合</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/s_l/2019/05/09/JDBCContion/" rel="next" title="JDBC数据库的连接">
                <i class="fa fa-chevron-left"></i> JDBC数据库的连接
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/s_l/2019/05/09/深入HashMap/" rel="prev" title="深入HashMap">
                深入HashMap <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏立鹏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/s_l/archives/">
              
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/www.86740.com/projects" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#集合框架概述"><span class="nav-number">1.</span> <span class="nav-text">集合框架概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#单列数据"><span class="nav-number">1.1.</span> <span class="nav-text">单列数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Collection的家族"><span class="nav-number">1.1.1.</span> <span class="nav-text">Collection的家族</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#迭代器"><span class="nav-number">1.1.2.</span> <span class="nav-text">迭代器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Collection常用方法"><span class="nav-number">1.1.3.</span> <span class="nav-text">Collection常用方法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#双列数据"><span class="nav-number">1.2.</span> <span class="nav-text">双列数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Map中的常用方法"><span class="nav-number">1.2.1.</span> <span class="nav-text">Map中的常用方法</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏立鹏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/s_l/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/s_l/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/s_l/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/s_l/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/s_l/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/s_l/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/s_l/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/s_l/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
